import { Injectable } from '@nestjs/common';
import { PrismaService } from '../../prisma/prisma.service';
import { CreateChildDto } from '../dto';

@Injectable()
export class ChildCareService {
  constructor(private prisma: PrismaService) {}
  async create(createChildDto: CreateChildDto, userId: number) {
    return await this.prisma.growthRecord.upsert({
      where: {
        user_id: userId,
        id: createChildDto.id ?? -1
      },
      create: {
        user_id: userId,
        height: createChildDto.height,
        weight: createChildDto.weight,
        headCircumference: createChildDto.headCircumference,
        doctorAdvice: createChildDto.doctorAdvice,
        recordedAt: new Date(createChildDto.recordedAt)
      },
      update: {
        height: createChildDto.height,
        weight: createChildDto.weight,
        headCircumference: createChildDto.headCircumference,
        doctorAdvice: createChildDto.doctorAdvice,
        recordedAt: new Date(createChildDto.recordedAt)
      }
    });
  }
  async findAll(query: { id?: number }, userId: number) {
    return this.prisma.growthRecord.findMany({
      where: {
        ...(query.id ? { id: Number(query.id) } : {}),
        user_id: userId
      },
      orderBy: {
        recordedAt: 'desc'
      }
    });
  }
}
